package com.czwx.bee.business.manager.modular.borrow.controller;


import com.czwx.bee.base.common.dto.ResponseDto;
import com.czwx.bee.base.common.exception.BizExceptionEnum;
import com.czwx.bee.base.common.utils.ShopOrderUtil;
import com.czwx.bee.base.web.common.BaseController;
import com.czwx.bee.business.manager.modular.borrow.dto.BorrowDto;
import com.czwx.bee.business.manager.modular.borrow.model.query.BorrowQuery;
import com.czwx.bee.business.manager.modular.borrow.service.BorrowService;
import com.czwx.bee.business.manager.modular.sys.model.SecurityUser;
import com.czwx.bee.business.manager.utils.SecurityUtil;
import io.swagger.annotations.*;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/borrow")
@Api(value = "BorrowController",tags = "借款控制器")
public class BorrowController extends BaseController {

    @Autowired
    private BorrowService borrowService;

    @PostMapping("/list")
    @ApiOperation(value = "分期借款订单列表", notes = "可分页条件查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "borrowQuery", value = "请输入查询信息", required = true, dataType = "BorrowQuery", paramType = "body") })
    @ApiResponses(
            @ApiResponse(code = 200,response = BorrowDto.class,message = "data：中返回list")
    )
    public ResponseDto list(@RequestBody BorrowQuery borrowQuery) throws Exception {
        SecurityUser securityUser=SecurityUtil.getSecurityUser();
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        Map params=objectToMap(borrowQuery);
        if(borrowQuery.getState()!=null){
            params.put("inState",ShopOrderUtil.getOrderStates(borrowQuery.getState()));
        }
        params.put("drainageId",securityUser.getDrainageId());
        PageQuery query=  new PageQuery(borrowQuery.getPage(),borrowQuery.getSize(),params);
        borrowService.getPageList(query);
        List<BorrowDto> list=query.getList();
        for(BorrowDto b:list){
            b.setStateStr(ShopOrderUtil.getOrderStateStr(b.getState()));
        }
        rs.setData(query);
        return rs;
    }
}
